package com.xinke.module.system.service.tenant;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.tenant.*;
import com.xinke.module.system.model.entity.tenant.TenantPackageDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.tenant.TenantPackageMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 租户套餐 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class TenantPackageServiceImpl implements TenantPackageService {

    @Resource
    private TenantPackageMapper tenantPackageMapper;

    @Override
    public Long createTenantPackage(TenantPackageSaveReqVO createReqVO) {
        // 插入
        TenantPackageDO tenantPackage = BeanUtils.toBean(createReqVO, TenantPackageDO.class);
        tenantPackageMapper.insert(tenantPackage);
        // 返回
        return tenantPackage.getPackageId();
    }

    @Override
    public void updateTenantPackage(TenantPackageSaveReqVO updateReqVO) {
        // 校验存在
        validateTenantPackageExists(updateReqVO.getPackageId());
        // 更新
        TenantPackageDO updateObj = BeanUtils.toBean(updateReqVO, TenantPackageDO.class);
        tenantPackageMapper.updateById(updateObj);
    }

    @Override
    public void deleteTenantPackage(Long packageId) {
        // 校验存在
        validateTenantPackageExists(packageId);
        // 删除
        tenantPackageMapper.deleteById(packageId);
    }

    private void validateTenantPackageExists(Long packageId) {
        if (tenantPackageMapper.selectById(packageId) == null) {
            throw exception(null);
        }
    }

    @Override
    public TenantPackageDO getTenantPackage(Long packageId) {
        return tenantPackageMapper.selectById(packageId);
    }

    @Override
    public PageResult<TenantPackageDO> getTenantPackagePage(TenantPackagePageReqVO pageReqVO) {
        return tenantPackageMapper.selectPage(pageReqVO);
    }

}